//
//  ViewController.m
//  SpringAnimationDemo
//
//  Created by zkingsoft on 16/2/16.
//  Copyright (c) 2016年 zkingsoft. All rights reserved.
//

#import "ViewController.h"

#define K_HEIGHT [UIScreen mainScreen].bounds.size.height
#define K_WEIDTH [UIScreen mainScreen].bounds.size.width

@interface ViewController (){
    UIView      *view1;
    UIView      *view2;
    UIView      *view3;
    UIView      *view4;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // cofigView
    view1 = [[UIView alloc] initWithFrame:CGRectMake(-20, -K_HEIGHT, K_HEIGHT+40, K_HEIGHT)];
    view1.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:view1];
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(0, K_HEIGHT-K_HEIGHT/4, K_WEIDTH+40, K_HEIGHT/4);
    [button1 setTitle:@"orangeColor" forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [view1 addSubview:button1];
    [button1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    view2 = [[UIView alloc] initWithFrame:CGRectMake(-20, -K_HEIGHT, K_HEIGHT+40, K_HEIGHT)];
    view2.backgroundColor = [UIColor blueColor];
    [self.view addSubview:view2];
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.frame = CGRectMake(0, K_HEIGHT-K_HEIGHT/4, K_WEIDTH+40, K_HEIGHT/4);
    [button2 setTitle:@"blueColor" forState:UIControlStateNormal];
    [button2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [view2 addSubview:button2];
    [button2 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    
    view3 = [[UIView alloc] initWithFrame:CGRectMake(-20, -K_HEIGHT, K_HEIGHT+40, K_HEIGHT)];
    view3.backgroundColor = [UIColor greenColor];
    [self.view addSubview:view3];
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    button3.frame = CGRectMake(0, K_HEIGHT-K_HEIGHT/4, K_WEIDTH+40, K_HEIGHT/4);
    [button3 setTitle:@"greenColor" forState:UIControlStateNormal];
    [button3 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [view3 addSubview:button3];
    [button3 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    
    view4 = [[UIView alloc] initWithFrame:CGRectMake(-20, -K_HEIGHT, K_HEIGHT+40, K_HEIGHT)];
    view4.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:view4];
    UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom];
    button4.frame = CGRectMake(0, K_HEIGHT-K_HEIGHT/4, K_WEIDTH+40, K_HEIGHT/4);
    [button4 setTitle:@"cyanColor" forState:UIControlStateNormal];
    [button4 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [view4 addSubview:button4];
    [button4 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

}

// colorbuttonClicked
- (void)buttonClicked:(UIButton *)button{
    CGRect frame = view1.frame;
    frame.origin.y -= K_HEIGHT;
    
    [UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        view4.frame = frame;
        view3.frame = frame;
        view2.frame = frame;
        view1.frame = frame;
    } completion:^(BOOL finished) {
        
    }];

}


// homeButtonClicked
- (IBAction)homeButtonClicked:(UIButton *)sender {
    
    [UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        
        // 第一个视图做动画
        CGRect frame = view1.frame;
        frame.origin.y += K_HEIGHT;
        view1.frame = frame;
    } completion:^(BOOL finished) {
        
        // 结束之后第二个做动画 第一个左摆
        [UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            CGRect frame = view2.frame;
            frame.origin.y += K_HEIGHT/4*3;
            view2.frame = frame;
            
            view1.transform = CGAffineTransformMakeRotation(M_PI/180*2);
        } completion:^(BOOL finished) {
            
            // 第三个做动画 第一个右摆 第二个左摆
            [UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                
                CGRect frame = view3.frame;
                frame.origin.y += K_HEIGHT/4*2;
                view3.frame = frame;
                
                view1.transform = CGAffineTransformMakeRotation(-M_PI/180*2);
                view2.transform = CGAffineTransformMakeRotation(M_PI/180*2);
            } completion:^(BOOL finished) {
                
                // 第四个做动画
                [UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                    
                    CGRect frame = view4.frame;
                    frame.origin.y += K_HEIGHT/4;
                    view4.frame = frame;
                    
                    view1.transform = CGAffineTransformMakeRotation(0);
                    view2.transform = CGAffineTransformMakeRotation(-M_PI/180*2);
                    view3.transform = CGAffineTransformMakeRotation(M_PI/180*2);
                } completion:^(BOOL finished) {
                    
                    [UIView animateWithDuration:0.4 animations:^{
                        view2.transform = CGAffineTransformMakeRotation(0);
                        view3.transform = CGAffineTransformMakeRotation(-M_PI/180*2);
                        view4.transform = CGAffineTransformMakeRotation(M_PI/180*2);
                    } completion:^(BOOL finished) {
                        [UIView animateWithDuration:0.4 animations:^{
                            view3.transform = CGAffineTransformMakeRotation(0);
                            view4.transform = CGAffineTransformMakeRotation(-M_PI/180*2);
                        } completion:^(BOOL finished) {
                            [UIView animateWithDuration:0.4 animations:^{
                                view4.transform = CGAffineTransformMakeRotation(0);
                            }];
                        }];
                    }];
                }];
            }];
        }];
    }];

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
